package com.aauedu.springboot;

import com.aauedu.springboot.bean.*;
import com.aauedu.springboot.config.BeanConfig;
import com.aauedu.springboot.config.BeanConfig3;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @SpringBootApplication:表示这是一个springboot应用/项目
 * @SpringBootApplication(scanBasePackages="com.aauedu")
 * scanBasePackages="com.aauedu"指定springboot要扫描的包和子包，
 * 如果有多个包，可以这样scanBasePackages={"com.aauedu","xxx","yyy"}
 *
 */
@SpringBootApplication(scanBasePackages = "com.aauedu")
public class MainApp {
    public static void main(String[] args) {
        //启动 SpringBoot 应用程序
        //返回ioc容器
        ConfigurableApplicationContext ioc = SpringApplication.run(MainApp.class, args);

        //查看容器里面的组件
//        String[] beanDefinitionNames = ioc.getBeanDefinitionNames();
//        for (String beanDefinitionName : beanDefinitionNames) {
//            System.out.println("beanDefinitionName---->" + beanDefinitionName);
//        }


//        A aBean = ioc.getBean(A.class);
//        System.out.println("aBean= " + aBean);

        //--演示在springboot 项目，依然可以使用spring的配置bean/注入bean/获取bean方式 start====
//        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//        Monster monster = context.getBean("monster03", Monster.class);
//        System.out.println("monster03---" + monster);
        //--演示在springboot 项目，依然可以使用spring的配置bean/注入bean/获取bean方式 end====

        //--演示在springboot 项目，依然可以使用spring的配置bean/注入bean/获取bean方式 end====

        //===演示 @Configuration start ====
//        Monster monster01 = ioc.getBean("monster01", Monster.class);
//        Monster monster02 = ioc.getBean("monster01", Monster.class);
//        System.out.println("monster01--" + monster01 + " " + monster01.hashCode());
//        System.out.println("monster02--" + monster02 + " " + monster02.hashCode());
        //===演示 @Configuration end ====


        //===演示 @Configuration start ====
//        BeanConfig bean = ioc.getBean(BeanConfig.class);
//        System.out.println("bean==" + bean);
        //===演示 @Configuration end ====

        //===演示@Configuration(proxyBeanMethods = xxx) start
        //1.先得到BeanConfig组件
//        BeanConfig beanConfig = ioc.getBean(BeanConfig.class);
//        Monster monster_01 = beanConfig.monster01();
//        Monster monster_02 = beanConfig.monster01();
//        System.out.println("monster_01=" + monster_01 + " " + monster_01.hashCode());
//        System.out.println("monster_02=" + monster_02 + " " + monster_02.hashCode());
        //特别说明: proxyBeanMethods 是在 调用@Bean方法 才生效，因此，需要先获取BeanConfig 组件，再调用方法
        //而不是直接通过SpringBoot主程序得到的容器来获取bean, 注意观察直接通过ioc.getBean()获取Bean, proxyBeanMethods值并没有生效
//        Monster monster_01 = ioc.getBean("monster01", Monster.class);
//        Monster monster_02 = ioc.getBean("monster01", Monster.class);
//        System.out.println("monster_01=" + monster_01 + " " + monster_01.hashCode());
//        System.out.println("monster_02=" + monster_02 + " " + monster_02.hashCode());
        //===演示@Configuration(proxyBeanMethods = xxx) end

        //===测试可以有多个配置类 start
//        Monster monster02 = ioc.getBean("monster02", Monster.class);
//        Monster monster01 = ioc.getBean("monster01", Monster.class);
//        System.out.println("monster02--" + monster02);
//        System.out.println("monster01--" + monster01);
        //===测试可以有多个配置类 end

        //===测试@Import 使用 start
//        Dog dogBean = ioc.getBean(Dog.class);
//        Cat catBean = ioc.getBean(Cat.class);
//        System.out.println("dogBean=" + dogBean + " " + dogBean.hashCode());
//        System.out.println("catBean=" + catBean + " " + catBean.hashCode());
        //===测试@Import 使用end

        //===演示 @ConditionalOnBean 使用 start ====
//        Dog dogBean = ioc.getBean("dog01", Dog.class);
//        System.out.println("dogBean=" + dogBean);
        //===演示 @ConditionalOnBean 使用 end ====

        //===演示 @ImportResources 使用 start ===
        Monster monster04 = ioc.getBean("monster04", Monster.class);
        System.out.println("monster04=" + monster04);
        System.out.println("monster04 bean是否存在->" + ioc.containsBean("monster04"));
        //===演示 @ImportResources 使用 end ===



    }
}
